package com.sky.d_guava;

import static org.junit.Assert.*;

import java.io.File;

import org.junit.Test;

import com.google.common.io.BaseEncoding;
import com.google.common.io.Files;

/**
 * http://blog.csdn.net/ac_dao_di/article/details/53750028
 * @Author:hjt
 */
public class Base64Demo {

	
	@Test
	public void encodeDecodeTest() throws Exception {
	    File file = new File("D:\\test2.txt");
	    byte[] bytes = Files.toByteArray(file);
	    BaseEncoding baseEncoding = BaseEncoding.base64();
	    //编码
	    String encoded = baseEncoding.encode(bytes);
//	    assertThat(Pattern.matches("[A-Za-z0-9+/=]+", encoded),is(true));
//	    assertThat(baseEncoding.decode(encoded),is(bytes));
	}
	
	/**
	 * guava base64 简单编码解码
	 */
	@Test
	public void testEncodeAndDecode() throws Exception {
		String source = "hello world";
		
		BaseEncoding baseEncoding = BaseEncoding.base64();
		
		String eStr = baseEncoding.encode(source.getBytes("UTF-8"));
		System.out.println(">>>" + eStr);
		
		byte[] bytes = baseEncoding.decode(eStr);
		String dt = bytes == null ? null : new String(bytes, "UTF-8");
		System.out.println(">>>" + dt);
//		
	}
	
	/**
	 public String decode(String s) {
	    return StringUtils.newStringUtf8(Base64.decodeBase64(s));
	}
	public String encode(String s) {
	    return Base64.encodeBase64String(StringUtils.getBytesUtf8(s));
	} 
	 */
}
